package cn.sciento.iam.saas.app.service.impl;

import cn.sciento.iam.domain.entity.Tenant;
import cn.sciento.iam.domain.repository.TenantRepository;
import cn.sciento.iam.saas.app.service.TenantService;
import cn.sciento.iam.saas.domain.service.TenantCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

@Service
public class TenantServiceImpl implements TenantService {
  private final TenantRepository tenantRepository;
  
  private final TenantCoreService tenantCoreService;
  
  @Autowired
  public TenantServiceImpl(TenantRepository tenantRepository, TenantCoreService tenantCoreService) {
    this.tenantRepository = tenantRepository;
    this.tenantCoreService = tenantCoreService;
  }
  
  @Override
  @Transactional(rollbackFor = {Exception.class})
  public Tenant createTenant(Tenant tenant) {
    return this.tenantCoreService.createTenant(tenant);
  }
  
  @Override
  @Transactional(rollbackFor = {Exception.class})
  public Tenant updateTenant(Long tenantId, Tenant tenant) {
    return this.tenantCoreService.updateTenant(tenantId, tenant);
  }
  
  @Override
  @Transactional(rollbackFor = {Exception.class})
  public int assignTenantToUser(Long tenantId, Long userId) {
    Assert.notNull(tenantId, "error.data_invalid");
    Assert.notNull(userId, "error.data_invalid");
    return this.tenantRepository.assignTenantToUser(tenantId, userId);
  }
}
